<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form action="/login">
  username<input type="text" name="username" ><br>
  password<input type="password" name="password" ><br>
  <input type="submit">
</form>
<h3>数据库中登录</h3>
<form action="/loginDb">
  username<input type="text" name="username" ><br>
  password<input type="password" name="password" ><br>
  <input type="submit">
</form>
  <h3>异步登录</h3>
  <form>
    username<input type="text" name="username" id="username"><br>
    password<input type="password" name="password" id="password"><br>
    <input type="button" value="异步" onclick="login_async()">
</form>
<script>
  const login_async=async () => {
    const username = document.querySelector("#username").value;
    const password = document.querySelector("#password").value;
    const data = JSON.stringify({username, password})

    const res = await fetch('/login_async', {
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      body: data
    });
    if (!res.ok) {
      alert('login fail');
      return;
    }
    const json = await res.json();
    if (json.code === 0) {
      alert('login suc');
      window.location.href = "/";
    } else {
      alert(json.msg);
    }
  }
</script>
</body>
</html>